/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package chameleon.model.formula;

import java.io.IOException;

/**
 * This class represents an iterator for a CNF formula. A CNF formula is the type :
 *
 * (a|| .. ||z) && (a|| .. ||z) && ...
 *
 * The tree structure of this kind of formula is
 *
 *                            &&
 *                          /    \
 *                         ||     a
 *                        /  \
 *                       a    a
 *
 * represents formula : (a||a)&&a
 *
 * @author simon
 */
public class CnfIterator {
    private Formula cnfFormula_;
    private Formula currentFormula_;

    public CnfIterator(Formula cnf){
        cnfFormula_ = cnf;
        currentFormula_ = cnf;
    }

    public boolean hasNext() throws IOException{
        return (!currentFormula_.isAtomic()
                && currentFormula_.getOperator().getTag() != Tag.OR);
    }

    public Formula getClause() throws IOException{
        if (currentFormula_.isAtomic())
            return currentFormula_;
        else if(currentFormula_.getOperator().getTag() == Tag.AND)
            return currentFormula_.getLeftF();
        else
            return currentFormula_;
    }

    public void next() throws IOException{
        currentFormula_ = cnfFormula_.getRightF();
    }
}
